package com.dpwgc.pan.controller;

import com.dpwgc.pan.service.UserService;
import com.dpwgc.pan.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Api("用户操作")
@RestController
@RequestMapping(value = "/User")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录
     * @param phone 用户手机号码
     * @param password 用户密码
     * @return
     */
    @ApiOperation("登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultUtil<Object> login(@RequestParam("phone") String phone,
                                    @RequestParam("password") String password){

        return userService.loginService(phone,password);
    }

    /**
     * 注册
     * @param phone 用户手机号码
     * @param nickName 用户昵称
     * @param password 用户密码
     * @param headImg 用户头像
     * @return
     */
    @ApiOperation("注册")
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public ResultUtil<Object> insertUser(@RequestParam("phone") String phone,
                                         @RequestParam("nickName") String nickName,
                                         @RequestParam("password") String password,
                                         @RequestParam("headImg") String headImg) throws Exception {

        return userService.insertUserService(phone,nickName, password,headImg);
    }

    /**
     * 根据用户id返回用户信息
     * @param userId 用户id
     * @return
     */
    @ApiOperation("根据用户id返回用户信息")
    @RequestMapping(value = "/queryUserById",method = RequestMethod.POST)
    public ResultUtil<Object> queryUserById(@RequestParam("userId") int userId){

        return userService.queryUserByIdService(userId);
    }

    /**
     * 根据用户id修改用户个人信息
     * @param userId
     * @return
     */
    @ApiOperation("用户修改个人信息")
    @RequestMapping(value = "/updateUserById",method = RequestMethod.POST)
    public ResultUtil<Object> updateUserById(@RequestParam("userId") int userId,
                                             @RequestParam("phone") String phone,
                                             @RequestParam("nickName") String nickName,
                                             @RequestParam("headImg") String headImg){

        return userService.updateUserByIdService(userId, phone, nickName, headImg);
    }

    /**
     * 更改密码
     * @param userId 用户id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    @ApiOperation("用户更改密码")
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    public ResultUtil<Object> updatePassword(@RequestParam("userId") int userId,
                                             @RequestParam("oldPassword") String oldPassword,
                                             @RequestParam("newPassword") String newPassword){

        return userService.updatePasswordService(userId, oldPassword, newPassword);
    }
}
